<?php

namespace App\Http\Controllers;

use Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
//use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\File;
use Illuminate\Http\Request;
use \App\Models\RecommandUser;

//用户操作

class UserhandleController extends Controller
{
    public function __construct() {
        
    }
    //分析师基本信息
    public function baseinfo(Request $request){
        $authinfo = '';
        $sessiondata = $request->session()->get('eai');
        if(!$sessiondata){
            if(isMobile()){
                return redirect('mlogin');
            }else{
                return redirect('login');
            }
        }
        $userid   = $sessiondata['id'];
        $baseinfo = DB::table('waihui_member')->where(array('id'=>$userid))->first();
        $authinfo = DB::table('waihui_member_info')->where(array('userid'=>$userid))->first();
        if(isMobile()){
        	return view('mobile.settings',compact('baseinfo','authinfo'));
        }else{
        	return view('userhandle.baseinfo',compact('baseinfo','authinfo'));
        }
    }
    //头像上传
    public function photohandle(Request $request){
        $sessiondata = $request->session()->get('eai');
        if(!$sessiondata){
            echo json_encode(array('status'=>1,'msg'=>'用户未登录'));
            return;
        }
        $userid   = $sessiondata['id'];
        $data['photo']  = trim($request->photo);
        $oldPhoto = DB::table('waihui_member')->where(array('id'=>$userid))->value('photo');
        Storage::append('photo.txt', '用户：'.$sessiondata['tel'].'， 原先的头像：'.$oldPhoto.',新上传的头像：'.$data['photo'].",时间".time());//缓存数据
//        if($oldPhoto && strpos($oldPhoto,'/uploads') == 0){ //如果用户重置头像，那么删除原来的头像（只删除用户自己上传的）
//            $filename = base_path().'/public'.$oldPhoto;
//            if(File::exists( $filename )){
//                File::delete( $filename );
//            }
//        }
        $id = DB::table('waihui_member')->where(array('id'=>$userid))->update($data);
        if($id){
            $request->session()->put('eai',array('id'=>$userid,'tel'=>$sessiondata['tel'],'username'=>$sessiondata['username'],'photo'=>$data['photo']));
            echo json_encode(array('status'=>0,'msg'=>'头像设置成功。'));
            return ;
        }else{
            echo json_encode(array('status'=>2,'msg'=>'头像设置失败。'));
            return ;
        }
    }
   //基本信息设置
    public function baseinfohandle(Request $request){
        $sessiondata = $request->session()->get('eai');
        if(!$sessiondata){
            echo json_encode(array('status'=>1,'msg'=>'用户未登录'));
            return;
        }
        $userid   = $sessiondata['id'];
        $data['username']      = trim($request->username);
        $data['sex']           = trim($request->sex);
        $data['birthday']      = strtotime(trim($request->birthday));
        $data['server_name']   = trim($request->server_name);
        $data['email']         = trim($request->email);
        $data['introduction']  = trim($request->introduction);
        $authdata['province']  = trim($request->province);
        $authdata['city']      = trim($request->city);
        $authdata['country']   = trim($request->country);
        $authdata = array_filter($authdata);
        $type = trim($request->type);
        if($type == 'tiyan'){
            $data['hasapply'] = 1;
            $data['applytime'] = time();
        }
        $id = DB::table('waihui_member')->where(array('id'=>$userid))->update($data);
        $isexist = DB::table('waihui_member_info')->where(array('userid'=>$userid))->first();
        if(!$isexist){
            $authdata['userid'] = $userid;
            if($authdata) {
                DB::table('waihui_member_info')->insert($authdata);
            }
        }else{
            if($authdata) {
                DB::table('waihui_member_info')->where(array('userid' => $userid))->update($authdata);
            }
        }
        $request->session()->put('eai',array('id'=>$userid,'tel'=>$sessiondata['tel'],'username'=>$data['username'],'photo'=>$sessiondata['photo']));
        if($type == 'tiyan'){
            echo json_encode(array('status'=>2,'msg'=>'申请成功！'));
            return ;
        }else{
            echo json_encode(array('status'=>0,'msg'=>'基本信息设置成功。'));
            return ;
        }
        
    }
    //分析师认证
    public function auth(Request $request){
        $sessionData = $request->session()->get('eai');
       
        if(!$sessionData){
            if(isMobile()){
                return redirect('mlogin');
            }else{
                return redirect('login');
            }
        }
        $tiyan = getenv('tiyan');
        $type = $request->get('id');  //tiyan ：点击申请体验账户按钮，进入个人资料中，默认显示申请体验账户tab   no:未完成资料
        if(!$type){
            $type = '';
        }
        
        //判断有没有该分析师
        $userinfo = DB::table('waihui_member')->select('*')->where('id','=',$sessionData['id'])->first();
        
        if(!$userinfo){
            return redirect('bdlists');
        }else{
           $moreinfo = DB::table('waihui_member as m')
                    ->join('waihui_member_info as i','m.id','=','i.userid')                   
                    ->select('i.province','i.city','i.country')
                    ->where('m.id','=',$sessionData['id'])
                    ->first();
            if(!$moreinfo){
               $moreinfo = '';
            }
        }
       
        $userinfo->reg_time = date('Y-m-d H:i:s',$userinfo->reg_time);
        
        //总收益
        $allprofit = allProfitData($userinfo->tel);
//        $allprofit = DB::table('waihui_order')
//                    ->select(DB::raw('sum(final_point) as allprofit'))
//                    ->where('tel','=',"$userinfo->tel")
//                    ->pluck('allprofit');
//        
//        $allprofit = $allprofit[0] ? $allprofit[0] : 0;
        
        $gendanProfit = gendanAllProfit($sessionData['tel']); 
        $gendanData = gendanAllData($sessionData['tel']);
        
        
        $userid      = $sessionData['id'];
        $authstatus  = DB::table('waihui_member')->where(array('id'=>$userid))->value('authstatus'); //认证状态
        $authinfo    = DB::table('waihui_member_info')->where(array('userid'=>$userid))->first(); //认证信息
//        if($type == 'no'){
//            return view('userhandle.completeinfo',compact('tiyan','type','gendanData','gendanProfit','allprofit','moreinfo','userinfo','authstatus','authinfo'));
//        }
        //判断是否发布过推荐榜单文章
        $RecommandUser = new RecommandUser();
        $tuijianStatus = $RecommandUser->isSetNickName($userid);
        //dd($tuijianStatus);
//        $tuijianStatus = count($tuijianStatus)>0 ? 1 : 0;
        $listidarr = [];
        $listid    = 0;
        if($type == "recomm"){
            $listid    = intval(trim($request->listid));
            $listidarr = DB::table("waihui_tuijian_list as l")
                            ->select("l.*")
                            ->where("l.id",$listid)->first();
            if($listidarr){
                $listidarr->analystarr = DB::table("waihui_member")
                                            ->whereIn('id',explode(',',$listidarr->analyst_id))
                                            ->select('username', 'photo','id')->get()->toArray();
            }
        }
        if(isMobile()){
                return view('mobile.modify',compact('tiyan','type','gendanData','gendanProfit','allprofit','moreinfo','userinfo','authstatus','authinfo'));
        }else{
                return view('userhandle.auth',compact('tuijianStatus','tiyan','type','gendanData','gendanProfit','allprofit','moreinfo','userinfo','authstatus','authinfo',"listidarr",'listid'));
        }   
    }
    
    //分析师认证操作
    public function cardAuth(Request $request){
        $sessiondata = $request->session()->get('eai');
        if(!$sessiondata){
            echo json_encode(array('status'=>2,'msg'=>'用户未登录'));
            return;
        }
        $userid = $sessiondata['id'];
        $data['username']       = trim($request->username);
        $data['idcard']         = trim($request->idcard);
        $data['idcardimg']      = trim($request->idcardimg);
        $data['idcardimgf']     = trim($request->idcardimgf);
        $data['card_auth_time'] = strtotime('now');
        $iscard = $this->isCreditNo($data['idcard']);
        if(!$iscard){
            echo json_encode(array('status'=>1,'msg'=>'您输入的身份证号不正确'));
            return ;
        }
        $isexist = DB::table('waihui_member_info')->where(array('userid'=>$userid))->first();
        if(!$isexist){
            $data['userid']  = $sessiondata['id'];
            $id = DB::table('waihui_member_info')->insert($data);
            if($id){
                DB::table('waihui_member')->where(array('id'=>$userid))->update(array('authstatus'=>'1'));
                echo json_encode(array('status'=>0,'msg'=>'认证提交成功，请等待审核。'));
                return ;
            }else{
                echo json_encode(array('status'=>2,'msg'=>'认证提交失败。'));
                return ;
            }
        }else{
            $oldIdCard = DB::table('waihui_member_info')->where(array('userid'=>$userid))->value('idcardimg');
            $oldIdCardf = DB::table('waihui_member_info')->where(array('userid'=>$userid))->value('idcardimgf');
            
            //身份证正面
            if($oldIdCard && strpos($oldIdCard,'/uploads/card') == 0){ //如果用户重置身份证，那么删除原来的
                $idcard_z = base_path().'/public'.$oldIdCard;
                if(File::exists( $idcard_z )){
                    File::delete( $idcard_z );
                }
            }
            //身份证反面
            if($oldIdCardf && strpos($oldIdCardf,'/uploads/card') == 0){ //如果用户重置身份证，那么删除原来的
                $idcard_f = base_path().'/public'.$oldIdCardf;
                if(File::exists( $idcard_f )){
                    File::delete( $idcard_f );
                }
            }
            $id = DB::table('waihui_member_info')->where(array('userid'=>$userid))->update($data);
            if($id){
                DB::table('waihui_member')->where(array('id'=>$userid))->update(array('authstatus'=>'1'));
                echo json_encode(array('status'=>3,'msg'=>'认证修改成功，请等待审核。'));
                return ;
            }else{
                echo json_encode(array('status'=>4,'msg'=>'未修改认证信息。'));
                return ;
            }
        }
    }
    //学历信息操作
    public function schoolAuth(Request $request){
        $sessiondata = $request->session()->get('eai');
        if(!$sessiondata){
            echo json_encode(array('status'=>2,'msg'=>'用户未登录'));
            return;
        }
        $userid = $sessiondata['id'];
        $data['diplomas']         = trim($request->diplomas);
        $data['school']           = trim($request->school);
        $data['school_auth_time'] = strtotime('now');
        $isexist = DB::table('waihui_member_info')
                 ->select('id','diplomas','school','school_auth_time')
                 ->where(array('userid'=>$userid))
                 ->first();
        //dd($isexist);
        if($isexist->id == ''){
            $data['userid']  = $sessiondata['id'];
            $id = DB::table('waihui_member_info')->insert($data);
            if($id){
                echo json_encode(array('status'=>0,'msg'=>'学历信息提交成功。','diplomas'=>$data['diplomas'],'school'=>$data['school']));
                return ;
            }else{
                echo json_encode(array('status'=>2,'msg'=>'学历信息提交失败。'));
                return ;
            }
        }else{
            $id = DB::table('waihui_member_info')->where(array('userid'=>$userid))->update($data);
            if($id){
                echo json_encode(array('status'=>3,'msg'=>'学历信息修改成功。','diplomas'=>$data['diplomas'],'school'=>$data['school']));
                return ;
            }else{
                echo json_encode(array('status'=>4,'msg'=>'未修改学历信息。','diplomas'=>$data['diplomas'],'school'=>$data['school']));
                return ;
            }
        }
    }
    //提现页面
    public function cashHandle(Request $request){
        $sessionData  = $request->session()->get('eai');
        if(!$sessionData){
            return view('user.login');
        }
        $tiyan = getenv('tiyan'); //申请体验账户
        $userinfo = DB::table('waihui_member')->select('username','authstatus','isanalyst','isvip','jifen','id','applytime','sex','email','birthday','server_name','tel','hasapply')->where('id','=',$sessionData['id'])->first();
        $moreinfo = DB::table('waihui_member_info')->select('username','idcard','idcardimg','idcardimgf','province','city','country')->where('userid','=',$sessionData['id'])->first();
        if(!$moreinfo){
            $ziliaotype = 1;  //资料未全部完善
        }else if($moreinfo->username == '' || $userinfo->sex == '' || $userinfo->email == ''|| $userinfo->birthday == '' || $moreinfo->province == '' || $moreinfo->city == '' || $moreinfo->country == '' || $userinfo->server_name == '' || $moreinfo->idcard == '' || $moreinfo->idcardimg == '' || $moreinfo->idcardimgf == ''){
            $ziliaotype = 1;  //资料未全部完善
        }else{
            $ziliaotype = 0;
        }
        $allprofit = allProfitData($userinfo->tel);
//        $allprofit = DB::table('waihui_order')
//                    ->select(DB::raw('sum(final_point) as allprofit'))
//                    ->where('tel','=',"$userinfo->tel")
//                    ->pluck('allprofit');
//       
//        $allprofit = $allprofit[0] ? $allprofit[0] : 0;
        
        
        $gendanProfit = gendanAllProfit($userinfo->tel); 
        
//        $gendanProfit = DB::table('waihui_gendan')  //订阅总收益
//                        ->select(DB::raw('(sum(end_point)-sum(begin_point)) as num'))
//                        ->where([['isstop','=','0'],['follower_tel','=',"$userinfo->tel"]])
//                        ->pluck('num');
//        if($gendanProfit && $gendanProfit[0]){
//            $gendanProfit = round($gendanProfit[0],2);
//        }else{
//            $gendanProfit = 0;
//        }
        $banklist     = UserhandleController::bankList($sessionData['id']);//银行卡信息
        $weekdata     = UserhandleController::weekRankingData($sessionData['id']);//周赛收益
        $weekprofit   = $weekdata[0];
        $weekcountbonus = $weekdata[1];
        $handandata   = UserhandleController::handanProfit($sessionData['tel'],$sessionData['id']);//策略收益
        $handanprofit = $handandata[0];
        $handanbonus  = $handandata[1];
        $drawmoney    = DB::table('waihui_withdraw')->select(DB::raw('sum(money) as money'))->where(array('userid'=>$sessionData['id']))->value('money'); //已提取金额
        return view('userhandle.cash',compact('userinfo','moreinfo','tiyan','allprofit','gendanProfit','ziliaotype','banklist','weekprofit','weekcountbonus','handanprofit','handanbonus','drawmoney'));
    }
    //提现处理
    public function bankAuth(Request $request){
        $sessiondata = $request->session()->get('eai');
        if(!$sessiondata){
            echo json_encode(array('status'=>1,'msg'=>'用户未登录'));
            return;
        }
        $count = DB::table('waihui_bank')->where(array('userid'=>$sessiondata['id']))->count();
        $max   = 3;//最多添加银行卡数
        if($count >= $max){
            echo json_encode(array('status'=>3,'msg'=>'您添加的银行卡数量已达上限'));
            return;
        }
        $bankid = trim($request->bankid);
        $data['bank_number']    = trim($request->bank_number);
        $data['bank']           = trim($request->bank);
        $data['bank_branch']    = trim($request->bank_branch);
        $data['bankimg']        = trim($request->bankimg);
        $data['bank_province']  = trim($request->bank_province);
        $data['bank_city']      = trim($request->bank_city);
        $data['bank_auth_time'] = strtotime('now');
        $data['userid']         = $sessiondata['id'];
        if($bankid){ //修改银行卡信息
            $isexist = DB::table('waihui_bank')->where(array('id'=>$bankid))->first();
            if($isexist && ($isexist->userid == $sessiondata['id'])){
                DB::table('waihui_bank')->where(array('id'=>$bankid))->update($data);
                echo json_encode(array('status'=>0,'msg'=>'银行卡信息修改成功。'));
                return ;
            }else{
                echo json_encode(array('status'=>2,'msg'=>'银行卡信息修改失败。'));
                return ;
            }
        }else{ //添加银行卡信息
            $id = DB::table('waihui_bank')->insert($data);
            if($id){
                echo json_encode(array('status'=>0,'msg'=>'银行卡信息提交成功。'));
                return ;
            }else{
                echo json_encode(array('status'=>2,'msg'=>'银行卡信息提交失败。'));
                return ;
            }
        }
    }
    //周赛收益
    public function weekRankingData($userid){
        $data     = DB::table('waihui_active_ranking')
                        ->select('*',DB::raw("FROM_UNIXTIME(week_time, '%Y年%m月') AS months"),DB::raw("FROM_UNIXTIME(week_time, '%w') AS weeks"))
                        ->where(array('userid'=>$userid,'status'=>2))
                        ->orderBy('months','desc')
                        ->get()->toArray();
        $rankdata   = [];
        $total      = 0;
        $monthtotal = 0;
        
        if($data){
            foreach ($data as $key=>$val){
               $aa = $key>0?$key-1:0;
               if($val->draw_status == 1){
                   if(($val->months != $data[$aa]->months) && ($key>0)){
                       $monthtotal = 0+$val->actual_bonus; //每个月第一次从0开始加
                   }else{
                       $monthtotal = $monthtotal+$val->actual_bonus; //每个月的每周相加
                   }
               }
               $rankdata[$val->months]['monthtotal'] = $monthtotal;
               $rankdata[$val->months]['monthdata'][] = $val;
               $total = $total+$val->actual_bonus;
            }
        }
        return array($rankdata,$total);
    }
    //周赛领取
    public function weekDraw(Request $request){
        $sessionData  = $request->session()->get('eai');
        if(!$sessionData){
            return view('user.login');
        }
       $isexist = DB::table('waihui_bank')->where(array('userid'=>$sessionData['id']))->first();
       if(!$isexist){
           echo json_encode(array('status'=>3,'msg'=>'您还未添加银行卡'));
           return ;
       }
       $rankingid   = trim($request->rankingid);
       $bankid      = trim($request->bankid);
       $rankinginfo = DB::table('waihui_active_ranking')->where(array('id'=>$rankingid,'userid'=>$sessionData['id'],'draw_status'=>1,'status'=>2))->first();
        if($rankinginfo){ //判断是否领取过
            $result = DB::table('waihui_active_ranking')->where(array('id'=>$rankingid))->update(array('draw_status'=>2));
            if($result){
                DB::table('waihui_withdraw')->insertGetId(array('userid'=>$sessionData['id'],'money'=>$rankinginfo->actual_bonus,'status'=>1,'draw_time'=>time(),'type'=>1,'draw_id'=>$rankingid,'bank_id'=>$bankid));
                echo json_encode(array('status'=>0,'msg'=>'领取成功'));
                return ;
            }else{
                echo json_encode(array('status'=>1,'msg'=>'领取失败'));
                return ;
            }
        }else{
            echo json_encode(array('status'=>2,'msg'=>'您已领取过'));
            return ;
        }
    }
    //策略收益
    public function handanProfit($tel,$userid){
        $where['tel']          = "$tel";
        $where['ispersonal']   = 0;
        $where['isstop']       = 0;
        $where['trade_status'] = '1';
        $data = DB::table('waihui_order')
                    ->select('tradeid','tel','begin_point','end_point','end_time','buy_status','typename','profit_point','loss_point','final_point','money',DB::raw("FROM_UNIXTIME(end_time, '%Y年%m月') AS months"))
                    ->where($where)
                    ->where('money','>',0)
                    ->orderBy('end_time','desc')->get()->toArray();
        $handanlist = [];
        $total      = 0;
        $monthtotal = 0;
        if($data){
            foreach ($data as $key=>$val){
                $aa = $key>0?$key-1:0;
                if (($val->months != $data[$aa]->months) && ($key > 0)) {
                    $monthtotal = 0 + $val->money; //每个月第一次从0开始加
                } else {
                    $monthtotal = $monthtotal + $val->money; //每个月的每一单相加
                }
                $monthsstatus = DB::table('waihui_withdraw')->where(array('userid'=>$userid,'type'=>2,'draw_id'=>$val->months))->first();
                if($monthsstatus){
                    $handanlist[$val->months]['monthstatus'] = $monthsstatus->status;
                }else{
                    $handanlist[$val->months]['monthstatus'] = 0;
                }
                $handanlist[$val->months]['monthtotal'] = $monthtotal;
                $handanlist[$val->months]['monthdata'][] = $val;
                $total = $total+$val->money;
            }
        }
        return array($handanlist,$total);
    }
    //策略领取
    public function handanDraw(Request $request){
        $sessionData  = $request->session()->get('eai');
        if(!$sessionData){
            return view('user.login');
        }
        $isexist = DB::table('waihui_bank')->where(array('userid'=>$sessionData['id']))->first();
        if(!$isexist){
            echo json_encode(array('status'=>3,'msg'=>'您还未添加银行卡'));
            return ;
        }
        $months   = trim($request->months);
        $money    = trim($request->money);
        $bankid   = trim($request->bankid);
        $year     = substr(preg_replace('/[^\d]+/',"-",$months),0,4);
        $month    = substr(preg_replace('/[^\d]+/',"-",$months),5,-1);
        $where['tel']          = $sessionData['tel'];
        $where['ispersonal']   = 0;
        $where['isstop']       = 0;
        $where['trade_status'] = '1';
        $beginendtime = UserhandleController::mFristAndLast($year,$month);
        $totalmoney = DB::table('waihui_order')->select(DB::raw('SUM(money) as totalmoney'))->where($where)->whereBetween('end_time', [$beginendtime['firstday'], $beginendtime['lastday']])->where('money','>',0)->pluck('totalmoney');
        if($totalmoney[0] == $money){ //判断钱数是否正确
            $exist = DB::table('waihui_withdraw')->where(array('draw_id'=>"$months",'userid'=>$sessionData['id'],'type'=>2))->first();
            if($exist){ //判断是否领取过
                echo json_encode(array('status'=>1,'msg'=>'该月份奖励您已领取过'));
                return ;
            }else{
                $id = DB::table('waihui_withdraw')->insertGetId(array('draw_id'=>"$months",'userid'=>$sessionData['id'],'type'=>2,'draw_time'=>time(),'status'=>1,'money'=>$money,'bank_id'=>$bankid));
                if($id){
                    echo json_encode(array('status'=>0,'msg'=>'策略奖励领取成功。'));
                    return ;
                }else{
                    echo json_encode(array('status'=>2,'msg'=>'策略奖励领取失败。'));
                    return ;
                }
            }
        }else{
            echo json_encode(array('status'=>1,'msg'=>'该月份无法领取'));
            return ;
        }
    }
    //删除银行卡
    public function selectBank(Request $request){
        $bankid      = trim($request->bankid);
        $isexist     = DB::table('waihui_bank')->where(array('id'=>$bankid))->first();
        $sessiondata = $request->session()->get('eai');
        if($isexist->userid == $sessiondata['id']){
            DB::table('waihui_bank')->where(array('userid'=>$sessiondata['id']))->update(array('status'=>1));
            $istrue = DB::table('waihui_bank')->where(array('id'=>$bankid))->update(array('status'=>2));
            if($istrue){
                echo json_encode(array('status'=>0,'msg'=>'设置成功'));
                return ;
            }else{
                echo json_encode(array('status'=>1,'msg'=>'设置失败'));
                return ;
            }
        }else{
            echo json_encode(array('status'=>2,'msg'=>'银行卡信息不正确'));
            return ;
        }
    }
    //银行卡列表
    public function bankList($userid){
        return DB::table('waihui_bank')->select('*')->where(array('userid'=>$userid))->orderBy('status','desc')->orderBy('id','desc')->get()->toArray();
    }
    //删除银行卡
    public function delBank(Request $request){
        $bankid      = trim($request->bankid);
        $isexist     = DB::table('waihui_bank')->where(array('id'=>$bankid))->first();
        $sessiondata = $request->session()->get('eai');
        if($isexist->userid == $sessiondata['id']){
            $istrue = DB::table('waihui_bank')->where(array('id'=>$bankid))->delete();
            if($istrue){
                echo json_encode(array('status'=>0,'msg'=>'银行卡删除成功'));
                return ;
            }else{
                echo json_encode(array('status'=>1,'msg'=>'银行卡删除失败'));
                return ;
            }
        }else{
            echo json_encode(array('status'=>2,'msg'=>'银行卡信息不正确'));
            return ;
        }
    }
    /**
     * ajax上传文件
     */
    public function uploadImages(Request $request)
    {
        if ($request->ajax()) {
            $file = $request->file('image');
            if(!$file->isValid()){//上传是否有效
                return response()->json(array('msg' => '上传失败','status'=>0));
            }
            $arr  = ['jpg','png','gif','jpeg'];
            if(!in_array($file->getClientOriginalExtension(),$arr)){
                return response()->json(array('msg' => '图片格式不正确','status'=>0));
            }
            
            $type = $request->type;
            if($type == 1){
                $url = 'photo';
            }elseif($type == 2){
                $url = 'card';
            }elseif($type == 3){
                $url = 'bank';
            }else{
                $url = 'photo';
            }
            
            // 第一个参数代表目录, 第二个参数代表我上方自己定义的一个存储媒介
            $path = $file->store($url, 'uploadsimage');
            return response()->json(array('msg' => "http://".$_SERVER['SERVER_NAME'].'/uploads/'.$path,'status'=>1));
        }else{
            return response()->json(array('msg' => '请选择上传图片','status'=>0));
        }
    }
    /**
     * 判断是否为合法的身份证号码
     * @param $mobile
     * @return int
     */
    function isCreditNo($vStr){
        $vCity = array(
            '11','12','13','14','15','21','22',
            '23','31','32','33','34','35','36',
            '37','41','42','43','44','45','46',
            '50','51','52','53','54','61','62',
            '63','64','65','71','81','82','91'
        );
        if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $vStr)) return false;
        if (!in_array(substr($vStr, 0, 2), $vCity)) return false;
        $vStr = preg_replace('/[xX]$/i', 'a', $vStr);
        $vLength = strlen($vStr);
        if ($vLength == 18) {
            $vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2);
        } else {
            $vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2);
        }
        if (date('Y-m-d', strtotime($vBirthday)) != $vBirthday) return false;
        if ($vLength == 18) {
            $vSum = 0;
            for ($i = 17 ; $i >= 0 ; $i--) {
                $vSubStr = substr($vStr, 17 - $i, 1);
                $vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr , 11));
            }
            if($vSum % 11 != 1) return false;
        }
        return true;
    }
    /**
     * 获取指定月份的第一天开始和最后一天结束的时间戳
     *
     * @param int $y 年份 $m 月份
     * @return array(本月开始时间，本月结束时间)
     */
    function mFristAndLast($y="",$m=""){
        if($y=="") $y=date("Y");
        if($m=="") $m=date("m");
        $m=sprintf("%02d",intval($m));
        $y=str_pad(intval($y),4,"0",STR_PAD_RIGHT);

        $m>12||$m<1?$m=1:$m=$m;
        $firstday=strtotime($y.$m."01000000");
        $firstdaystr=date("Y-m-01",$firstday);
        $lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));
        return array("firstday"=>$firstday,"lastday"=>$lastday);
    }
    
    
}
